//分页的服务
var app = angular.module('App', ['angularFileUpload']);
app.service('common', function() {
    this.pageshow = function(pageon, pages) {
        var pageArr = []
        if (pages > 7) {
            if (3 < pageon && pageon < pages - 2) {
                pageArr = [];
                pageArr = [pageon - 3, pageon - 2, pageon - 1, pageon, pageon + 1, pageon + 2, pageon + 3];
            } else if (4 > pageon) {
                pageArr = [1, 2, 3, 4, 5, 6, 7];
            } else if (pages - 3 < pageon) {
                pageArr = [pages - 6, pages - 5, pages - 4, pages - 3, pages - 2, pages - 1, pages];
            }
        } else {
            pageArr = [];
            for (var i = 0; i < pages; i++) {
                pageArr.push(i + 1);
            }
        }
        return pageArr;
    }

    //数组保存关键词
    this.saveSearchWord = function(arr) {
        var wordsarr = [];
        for (var i in arr) {
            if (arr[i] != '') {
                wordsarr.push(arr[i]);
            }
        }
        return wordsarr;
    }

    //返回处理过的url
    this.hanldeUrl = function(wordsArr,pageon) {
        var j = 0;
        var url = window.catalog.url;
        for (var i in wordsArr) {
            if (j == 0) {
                if(wordsArr[i]!=''){
                    url += '?' + i + '=' + wordsArr[i];
                    j++;
                }

            } else {
                if(wordsArr[i]!=''){
                    url += '&' + i + '=' + wordsArr[i];
                }
            }
        }
        if(pageon>0){
            if(j ==0){
                url += '?page='+pageon;
            }else{
                url += '&page='+pageon;
            }

        }
        return url;
    }

    //清空搜索条件
    this.clearSearch = function(wordsArr) {
        for (var i in wordsArr) {
            if (typeof(wordsArr) == 'number') {
                wordsArr[i] = 0;
            } else {
                wordsArr[i] = '';
            }
        }

        return wordsArr;
    }

    this.FilesToFileId = function (files){
        var FileId=[];
        if(files&&files instanceof Array&&files.length>0){

            for(var i in files){
                FileId.push(parseInt(files[i].Id));
            }
            return JSON.stringify(FileId);
        }else{
            return JSON.stringify(FileId);
        }
    }

});